<template>
  <div>
    <headerXX :name="title"></headerXX>
    <van-search
      v-model="XXyz.orderNumber"
      shape="round"
      background="none"
      placeholder="请输入订单号后五位信息"
      @search="onSearch"
      @clear="onSearch"
    />
    <van-tabs :active="active" @click="clickTab">
      <van-tab title="全部" name="0"></van-tab>
      <van-tab title="待使用" name="1"></van-tab>
      <van-tab title="待审核" name="2"></van-tab>
      <van-tab title="已取消" name="3"></van-tab>
      <van-tab title="已完成" name="4"></van-tab>
    </van-tabs>
    <van-pull-refresh
      class="pull-refresh_Search"
      v-model="refreshing"
      @refresh="onRefresh"
    >
      <van-empty
        v-if="list.length == 0 && !error"
        image="search"
        description="未找到数据"
      />
      <van-empty v-if="error" image="error" description="网络错误" />
      <van-list
        :immediate-check="false"
        v-model:loading="loading"
        :finished="finished"
        :error.sync="error"
        error-text="请求失败，点击重新加载"
        finished-text="没有更多了"
        @load="onLoad"
      >
        <!-- 列表内容 -->
        <div class="ulbox">
          <div
            class="ul"
            v-for="(item, index) in list"
            :key="index"
            @click="TZXQ(item)"
          >
            <div class="lost_title_list">
              <div style="display: flex; justify-content: space-between">
                <div style="width: 70%">
                  <div style="display: flex; align-items: center">
                    <div class="WZ_size_16 marginRight3">
                      {{ item.orderType }}
                    </div>

                    <van-image
                      v-if="item.orderStatus == 0"
                      class="img"
                      radius="4"
                      width="80"
                      height="25"
                      :src="require('../static/SY.png')"
                      mode=""
                    >
                    </van-image>
                    <van-image
                      v-if="item.orderStatus == -1"
                      class="img"
                      radius="4"
                      width="80"
                      height="25"
                      :src="require('../static/QX.png')"
                      mode=""
                    >
                    </van-image>
                    <van-image
                      v-if="item.orderStatus == 2"
                      class="img"
                      radius="4"
                      width="80"
                      height="25"
                      :src="require('../static/SH.png')"
                      mode=""
                    >
                    </van-image>
                    <van-image
                      v-if="item.orderStatus == 1"
                      class="img"
                      radius="4"
                      width="80"
                      height="25"
                      :src="require('../static/WC.png')"
                      mode=""
                    >
                    </van-image>
                    <van-image
                      v-if="item.orderStatus == 3"
                      class="img"
                      radius="4"
                      width="80"
                      height="25"
                      :src="require('../static/PJ.png')"
                      mode=""
                    >
                    </van-image>
                    <van-tag
                      class="BHtag"
                      v-if="item.orderStatus == 4"
                      type="primary"
                      color="#ffe1e1"
                      text-color="#ad0000"
                    >
                      <p class="yuan"></p>
                      已驳回
                    </van-tag>
                  </div>
                  <div class="name WZ_size_14">
                    {{ item.userName }}
                  </div>
                </div>
                <div
                  class="xxl"
                  v-if="
                    item.orderStatus != -1 &&
                    item.orderStatus != 1 &&
                    item.orderStatus != 3
                  "
                >
                  <van-image
                    class="img"
                    radius="4"
                    width="80"
                    height="80"
                    :src="item.img"
                  >
                    <template v-slot:loading>
                      <van-loading type="spinner" size="20" />
                    </template>
                  </van-image>
                </div>
              </div>

              <div class="lost_title">
                <van-icon name="clock" size="20" color="#0c7984" />
                <div class="lost_title1 WZ_size_10">时间：</div>
                <div class="XX WZ_size_10">{{ item.creationTime }}</div>
              </div>
              <div class="lost_title">
                <van-icon name="coupon" size="20" color="#0c7984" />
                <div class="lost_title1 WZ_size_10">订单号：</div>
                <div class="XX WZ_size_10">{{ item.orderNumber }}</div>
              </div>
              <van-divider
                v-if="item.orderStatus == 0"
                :style="{
                  borderColor: '#999',
                  margin: '15px 0',
                  marginBottom: '10px',
                }"
              >
              </van-divider>
              <div
                v-if="item.orderStatus == 0 || item.orderStatus == 4"
                style="display: flex; justify-content: space-between"
              >
                <div class=""></div>

                <van-button size="small" @click.stop="QXDD(item)" type="warning"
                  >取消订单</van-button
                >
                <!-- <u-button

                  style="border: none; color: rgb(56, 56, 56)"
                  :hairline="false"
                  color="rgb(229, 229, 229)"
                  text="取消订单"
                ></u-button> -->
              </div>
            </div>
          </div>
        </div>

        <template #error>
          <div class="error-tip" @click="retryLoad">
            <van-icon name="warning" />
            <span>加载失败，点击重试</span>
          </div>
        </template>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
import QRCode from "qrcode";
import headerXX from "../components/title.vue";
import { getmoreRoute } from "../common/moreRouteList";
import {
  getServiceOrderList,
  updateServiceOrderUser,
} from "@/api/realAssets/icsharedstation/manager.js";
import { selectUser } from "@/api/realAssets/icsharedstation/home.js";
import { getandroidCode } from "../common/auth";
import { Toast, Dialog } from "vant";
import { testUserMessage } from "../common/testUser.js";
export default {
  components: {
    headerXX,
  },
  data() {
    return {
      title: "订单列表",
      active: "0",
      list: [],
      loading: false,
      finished: false,
      refreshing: false,
      error: false, // 控制错误状态
      siteData: "",
      XXyz: {
        serviceType: "",
        orderNumber: "",
        mobile: "",
        pageNumber: 1,
        pageSize: 10,
        orderStatus: "",
        orderType: 1,
        postId: 0,
      },
    };
  },
  methods: {
    // 跳转详情页面
    TZXQ(val) {
      let li = encodeURIComponent(JSON.stringify(val));
      if (val.orderType == "疗养登记") {
        this.$router.push({
          name: "i_life_lydj_orderDetail",
          query: {
            YYXQxx: li,
          },
        });
      } else {
        this.$router.push({
          name: "rvicsharedstation-DD_DDXQ",
          query: {
            XX: li, // 序列化对象
          },
        });
      }
    },
    // 取消订单
    QXDD(val) {
      Dialog.confirm({
        title: "是否取消当前订单",
        message:
          "订单号为：【" + val.orderNumber + "】（取消后请等待管理员审核）",
      })
        .then(() => {
          updateServiceOrderUser({
            id: val.id,
          }).then((res) => {
            if (res.data == "SUCCESS") {
              Toast.success("申请已提交");
              this.onRefresh();
            }
          });
          // on confirm
        })
        .catch(() => {});
    },
    generateQRCode() {
      setTimeout(async () => {
        try {
          for (const item of this.list) {
            item.img = await QRCode.toDataURL(item.id);
          }
          // 强制重新赋值数组触发更新
          this.list = [...this.list];
        } catch (err) {}
      }, 10);
    },
    onSearch() {
      this.onRefresh();
    },
    // 清空搜索
    onCancel() {
      // console.log(XXyz.value.districtName);
      this.XXyz.orderNumber = "";
      this.onRefresh();
    },
    //默认获取用户信息
    getYH() {
      selectUser({
        ishid: getandroidCode(), //拿到编码
      }).then((resXX) => {
        if (resXX.data != "用户id错误") {
          this.XXyz.mobile = resXX.data.mobile;

          this.onLoad();
        }
      });
    },
    // 加载数据
    onLoad() {
      this.loading = true;
      if (this.error) return; // 如果当前是错误状态，不自动触发加载
      if (this.refreshing) {
        this.list = [];
      }
      this.XXyz.postId = this.siteData.postId;
      //  console.log("huoqu",this.XXyz);
      getServiceOrderList(this.XXyz).then((res) => {
        console.log(res);
        if (res.resultMessage != "SUCCESS") {
          this.list = [];
          this.error = true; // 显示错误提示
        } else if (res.data.length == 0) {
          this.list = [];
          this.finished = true;
        } else {
          this.list = [...this.list, ...res.data];
          // list.value = res.data;
          this.generateQRCode();
        }
        this.loading = false;
        // 判断是否已加载完所有数据
        if (this.list.length == res.pageNum) {
          this.finished = true;
        } else {
          this.XXyz.pageNumber++;
        }
        // 加载状态结束

        this.refreshing = false;
      });
    },
    // 下拉刷新
    onRefresh() {
      //   list.value = [];
      this.XXyz.pageNumber = 1;
      this.XXyz.pageSize = 10;
      this.finished = false;
      this.error = false;
      this.loading = true;
      this.refreshing = true;
      this.onLoad();
    },

    // 点击重试
    retryLoad() {
      this.error = false;
      this.loading = true;
      this.onLoad();
    },
    clickTab(e) {
      if (e == 0) {
        this.XXyz.orderStatus = "";
      } else if (e == 1) {
        this.XXyz.orderStatus = "0,4";
      } else if (e == 2) {
        this.XXyz.orderStatus = "2";
      } else if (e == 3) {
        this.XXyz.orderStatus = "-1";
      } else if (e == 4) {
        this.XXyz.orderStatus = "1,3";
      }

      this.onRefresh();
    },
  },
  mounted() {
    testUserMessage("共享药房").then((siteData) => {
      // console.log("5555",siteData[0]);
      this.siteData = siteData[0];
      // this.queryList();
      // if (this.$route.query.XX == "") {
      //   this.active = "0";
      // } else if (this.$route.query.XX == "0") {
      //   this.active = "1";
      // } else if (this.$route.query.XX == "2") {
      //   this.active = "2";
      // } else if (this.$route.query.XX == "-1") {
      //   this.active = "3";
      // } else if (this.$route.query.XX == "1" || this.$route.query.XX == "3") {
      //   this.active = "4";
      // }
      this.getYH();
    });
  },
};
</script>

<style lang="less" scoped>
@import "../static/css/GYcSS.less";
@import "../static/css/index.less";
.BHtag {
  width: 70px;
  height: 25px;
  box-sizing: border-box;
  overflow: hidden;
  // padding: 3px;
  .yuan {
    width: 5px;
    height: 5px;
    background: #ad0000;
    border-radius: 50%;
    margin: 0px 3px;
    margin-right: 8px;
  }
}
.error-tip {
  padding: 16px;
  color: #f56c6c;
  text-align: center;
}
.error-tip .van-icon {
  margin-right: 4px;
  vertical-align: middle;
}
.lost_title_list {
  width: 100%;
  margin: 0 auto;
  background: #ffffff;
  border-radius: 5px;
  //background-color: #f0f0f0;
  position: relative;
  line-height: 40px;
  margin-bottom: 10px;
  padding: 10px 20px;
  box-sizing: border-box;

  .title {
    font-size: 13px;
  }

  .name {
    color: rgb(158, 158, 158);
  }

  .XX {
    color: rgb(158, 158, 158);
    font-size: 12px;
  }

  // @include respond-to('phone') {
  // margin-bottom: 25rpx;
  // padding: 20rpx;
  // }

  // @include respond-to('android') {

  // 	padding: 40rpx;
  // }

  .lost_title {
    display: flex !important;
    flex-direction: row;
    // align-content: center;
    align-items: center;
    font-size: 15px;
    // @include respond-to('phone') {
    // 	font-size: 30rpx;
    // }

    // @include respond-to('android') {
    // 	font-size: 80rpx;
    // }

    .lost_title1 {
      font-weight: 700;
      margin-left: 5px;
    }
  }

  .lost-right {
    position: absolute;
    right: 5px;
    bottom: 10px;
    width: 30%;
    height: 100px;

    .lost-btn {
      background: rgba(2, 117, 113, 1);
      width: 50px;
      height: 30px;
      color: #ffffff;
      text-align: center;
      line-height: 20px;
      border-radius: 5px;
      margin-top: 5px;
    }
  }
}
.btn {
  width: 100px;
}
.xxl {
  width: 80px;
}
</style>
